#!/bin/sh
# runs browser with surfraw url through dmenu so one can choose
# can have multiple prefixes for choice of browser or surfraw
# function

PATH=/usr/lib/surfraw:/usr/share/surfraw:${HOME}/.config/surfraw/elvi:${PATH}

BROWSER=plumb
# xdotool search --onlyvisible --class  windowraise windowfocus
MARKFILE="$HOME/lib/bookmarks"
setbrowser() { BROWSER="$1"; url=$(echo "$url" | cut -c 4-); }

sort "$MARKFILE" | dmenu.sh -i -l 10 -p 'link' | while read -r url; do
[ ! "$url" ] && exit

case "$(echo "$url"|cut -d " " -f1)" in
    ff) setbrowser firefox ;;
    st) setbrowser surf_tor  ;;
esac

grep -q "$url" "$MARKFILE" && url=$(echo "$url" | sed 's/.* http/http/g')

args=$(echo $url | sed -e 's/^\w* //')
case $(echo "$url"| cut -d " " -f1) in
    *.*|*:*|*/*)	"$BROWSER" "$url" ;;
    gberg|G)		gutenberg	-browser="$BROWSER" $args ;;
    uesp|tes)		uesp		-browser="$BROWSER" $args ;;
    ddg|D)			duckduckgo	-browser="$BROWSER" $args ;;
    q)			qwant		-browser="$BROWSER" $args ;;
    aw|awiki)		archwiki	-browser="$BROWSER" $args ;;
    wi|wiki)		infogalactic	-browser="$BROWSER" $args ;;
    wq|wikiq)		wikiquote	-browser="$BROWSER" $args ;;
    imdb)			imdb		-browser="$BROWSER" $args ;;
    map)			google -m	-browser="$BROWSER" $args ;;
#   yt|youtube) 	sputube		-browser="$BROWSER" $args ;;
    yt|youtube) 	Yt 			$args ;;
    git|github)		github		-browser="$BROWSER" $args ;;
    ru)				rutracker	-browser="$BROWSER" $args ;;
    aarg)			aaaaarg		-browser="$BROWSER" $args ;;
    gen)			libgen		-browser="$BROWSER" $args ;;
    sp|S)			startpage	-browser="$BROWSER" $args ;;
    cr)				crossref	-browser="$BROWSER" $args ;;
    gr)				perseus		-browser="$BROWSER" $args ;;
    sw)				swisscows	-browser="$BROWSER" $args ;;
    *)				duckduckgo	-browser="$BROWSER" $url ;;
esac &
sleep 0.1
done
